Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Namespace BMLERP
	Friend Class frmINV110
		Inherits System.Windows.Forms.Form
		Dim OldWMWHC As String
		Sub ButtonStatus()
			cmdAddnew.Enabled = Not ISEDITING
			cmdEdit.Enabled = Not ISEDITING
			cmdSave.Enabled = ISEDITING	' And txtIMITN <> ""
			cmdDelete.Enabled = Not ISEDITING 'And txtIMITN <> ""
			cmdCancel.Enabled = ISEDITING
			'txtIMITN.Locked = Not ISADDING ' Lock item code tru truong hop ISADDING=true
			'txtDepartment.Enabled = ISEDITING
			'cboDepartment.Visible = Not ISADDING
			'txtDepartmentID.Visible = ISADDING
		End Sub

		Private Sub Control(ByRef GT As Object)
			txtWMWHC.Enabled = GT
			txtWMDSC.Enabled = GT
			cboWHCID.Enabled = GT

			txtWMWHC.ReadOnly = Not GT
			txtWMDSC.ReadOnly = Not GT
			cboWHCID.Locked = Not GT

			cboWMWHC.Enabled = Not GT
			cboWMWHC.Locked = GT
		End Sub
		Private Sub WriteToScreen(ByRef tri As Byte)
			Dim TBL As ADODB.Recordset
			If tri = 0 Then	'gan rong


				txtWMWHC.Text = ""
				txtWMDSC.Text = ""
				cboWHCID.BoundText = "'"

			Else
				TBL = New ADODB.Recordset
				TBL.Open("  Select IWM.*From IWM where  WMWHC ='" & cboWMWHC.BoundText & "'", DBconn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, ADODB.CommandTypeEnum.adCmdText)
				If TBL.EOF = False Then
					txtWMWHC.Text = IIf(IsDBNull(TBL.Fields("wmwhc").Value), "", TBL.Fields("wmwhc").Value)
					txtWMDSC.Text = IIf(IsDBNull(TBL.Fields("WMDSC").Value), "", TBL.Fields("WMDSC").Value)
					cboWHCID.BoundText = IIf(IsDBNull(TBL.Fields("whcid").Value), "", TBL.Fields("whcid").Value)
				Else
					cboWMWHC.BoundText = ""
					txtWMWHC.Text = ""
					txtWMDSC.Text = ""
					cboWHCID.BoundText = "'"
				End If
				TBL.Close()
			End If
			OldWMWHC = txtWMWHC.Text
		End Sub
		Private Sub WriteToFile(ByRef tri As Byte)
			Dim cmdExecute As Object
			Dim SQLInsert As String
			If tri = 0 Then
				SQLInsert = " INSERT INTO IWM " & "(WMWHC,WMDSC,WHCID) " & "VALUES " & "('" & VB.Left(txtWMWHC.Text, 2) & "', '" & VB.Left(txtWMDSC.Text, 30) & "', '" & cboWHCID.BoundText & "')"
			Else
				SQLInsert = "UPDATE IWM SET WMWHC = '" & VB.Left(txtWMWHC.Text, 2) & "', WMDSC ='" & VB.Left(txtWMDSC.Text, 30) & "', " & "WHCID ='" & cboWHCID.BoundText & "' WHERE WMWHC ='" & OldWMWHC & "'"


			End If

			cmdExecute = New ADODB.Command
			cmdExecute.ActiveConnection = DBconn
			cmdExecute.CommandText = SQLInsert
			cmdExecute.Execute()

		End Sub


		Private Sub cboWHCID_KeyPressEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_KeyPressEvent) Handles cboWHCID.KeyPressEvent
			If eventArgs.keyAscii = 13 Then
				cmdSave.Focus()

			End If

		End Sub

		Private Sub cboWMWHC_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboWMWHC.ClickEvent
			If eventArgs.area = 2 Then
				WriteToScreen((1))
			End If

		End Sub

		Private Sub cmdAddnew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddnew.Click
			ISADDING = True
			ISEDITING = True
			WriteToScreen((0))
			Control((True))
			' Ham Gan tri man hinh
			ButtonStatus()
			txtWMWHC.Focus()
		End Sub

		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
			' Gan tri man nhinh
			ISADDING = False
			ISEDITING = False
			Control((False))
			ButtonStatus()
			txtWMWHC.Text = cboWMWHC.BoundText
			WriteToScreen((1))

		End Sub

		Private Sub cmdDelete_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDelete.Click
			Dim cmdExecute As Object
			ISADDING = False
			ISEDITING = False
			' Ham delete theo Item chon
			Dim SQL As String
			SQL = "DELETE FROM IWM WHERE wmwhc = '" & txtWMWHC.Text & "'"

			cmdExecute = New ADODB.Command
			cmdExecute.ActiveConnection = DBconn
			cmdExecute.CommandText = SQL
			cmdExecute.Execute()
			Adodc1.Refresh()
			WriteToScreen((0))
			ButtonStatus()

		End Sub

		Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click
			ISEDITING = True
			' Ham gan tri man hinh
			ISADDING = False
			ISEDITING = True
			Control((True))

			ButtonStatus()

		End Sub

		Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
			Me.Close()
		End Sub

		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click
			' Ham Save tri vao file
			'Dim tbl_ChapNhan As ADODB.Recordset
			'Set tbl_ChapNhan = New ADODB.Recordset
			'tbl_ChapNhan.Open "  Select IIM.*From IIM ", DBconn, adOpenKeyset, adLockOptimistic, adCmdText
			'tbl_ChapNhan.Close
			If ISADDING = False Then
				WriteToFile((1))
			Else
				WriteToFile((0))
			End If
			Adodc1.Refresh()
			Control((False))
			'''''''''''
			ISADDING = False
			ISEDITING = False

			ButtonStatus()

		End Sub

		Private Sub frmINV110_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			If Not InitSuccess Then
				BIENCHUNG()
			End If

			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdTable
			Adodc1.RecordSource = "IWM"	'Warehour master
			Adodc1.Refresh()

			Adodc2.ConnectionString = ConnectString
			Adodc2.ConnectionTimeout = 10
			Adodc2.CommandType = ADODB.CommandTypeEnum.adCmdTable
			Adodc2.RecordSource = "ACN"	'company profile
			Adodc2.Refresh()
			ISADDING = False
			ISEDITING = False
			ButtonStatus()
			Me.VB6_AddADODataBinding()
			Me.cboWMWHC.RowSource = Me.ADOBind_Adodc1.DataSource
			Me.cboWHCID.RowSource = Me.ADOBind_Adodc2.DataSource
		End Sub

		Private Sub txtWMDSC_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtWMDSC.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				cboWHCID.Focus()

			End If

			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub

		Private Sub txtWMWHC_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtWMWHC.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				If ValidMaso(OldWMWHC, txtWMWHC, "IWM", " WMWHC='" & txtWMWHC.Text & "'") Then
					MsgBox("This code is already exist")
					txtWMWHC.Focus()
				Else
					txtWMDSC.Focus()
				End If

			End If

			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
	End Class
End Namespace